package com.panshi.controller.logic;

import com.panshi.api.UserApi;
import com.panshi.po.UserPO;
import com.panshi.sendEmail.SendEmailIdentifyServiceImp;
import com.panshi.vo.ResponseVO;
import com.panshi.vo.UserBean;
import com.panshi.vo.UserLogicVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Controller
@Slf4j
@RequestMapping("/user")
public class LogicController {

    @Autowired
    private UserApi userApi;

    @Autowired
    private RedisTemplate<String,String> template;

    /**
     * 校验图形验证码
     * @param userBean
     * @param request
     * @return
     */
    @PostMapping("/identify")
    @ResponseBody
    public Boolean checkIdentify(UserBean userBean, HttpServletRequest request){
        String ident = String.valueOf(request.getSession().getAttribute("VerifyCode"));
        System.out.println("identify:"+userBean.getIdentify());
        System.out.println("ident:"+ident);
        if(!ident.equals(userBean.getIdentify())){
            return false;
        }
        return true;
    }


    /**
     * 用户登录
     * @param userLogicVO
     * @param request
     * @return
     */
    @PostMapping("/logic")
    @ResponseBody
    public Map<Object,Object> logic(UserLogicVO userLogicVO, HttpServletRequest request){
        Map<Object,Object> map = new HashMap<>();
        request.getSession();
        String lockDown = template.opsForValue().get("lockedDown:"+userLogicVO.getEmail());
        log.info("被锁的用户邮箱号码：=====================》"+lockDown);
        if(null != lockDown){//表示当前状态为锁定状态
            //获取该状态的剩余时长
            Long lastTime = template.opsForValue().getOperations().getExpire("lockedDown:"+userLogicVO.getEmail());
            map.put("lastTime",lastTime);
            map.put("successes",false);//表示锁定状态
            return map;
        }
        log.info("---------------------------{}",userLogicVO);
        ResponseVO<UserPO> vo = userApi.userLogic(userLogicVO);
        if(null == vo.getData() || "".equals(vo.getData())){
            map.put("success",false);
            map.put("message","邮箱账号密码错误");
            map.put("successes",true);//表示不是锁定状态
        }else {
            //登录后将用户邮箱存入session域中
            request.getSession().setAttribute("logicEmail",userLogicVO.getEmail());
            map.put("success",true);
            map.put("message","登录成功");
            map.put("user",vo.getData());
            map.put("successes",true);//表示不是锁定状态
        }
        return map;
    }

    /**
     * 登录时验证码错误在10分钟内错误5次将被锁定30分钟
     * @param userBean
     * @return
     */
    @PostMapping("/identifyErrors")
    @ResponseBody
    public Map<Object,Object> errorIdentify(UserBean userBean){
        Map<Object,Object> map = new HashMap<>();
        String lockDown = template.opsForValue().get("lockedDown:"+userBean.getEmail());
        log.info("被锁用户=================>"+lockDown);
        if(null != lockDown){//表示当前状态为锁定状态
            //获取该状态的剩余时长
            Long lastTime = template.opsForValue().getOperations().getExpire("lockedDown:"+userBean.getEmail());
            map.put("lastTime",lastTime);
            map.put("success",false);
            return map;
        }
        //10分钟内验证码错误的次数
        String errorNumber = template.opsForValue().get("error："+userBean.getEmail());
        int number ;
        if(null == errorNumber){
            number = 0;
        }else {
            number = Integer.valueOf(errorNumber).intValue();
        }

        System.out.println("number:   "+number);
        if(number == 0){
            template.opsForValue().set("error："+userBean.getEmail(),"1");
            template.opsForValue().set("error："+userBean.getEmail(),"1",600, TimeUnit.SECONDS);
        }else {
            Long time = template.opsForValue().getOperations().getExpire("error："+userBean.getEmail());
            template.opsForValue().set("error："+userBean.getEmail(),String.valueOf(number+1),time,TimeUnit.SECONDS);
        }
        if(number == 4){
            template.opsForValue().set("lockedDown:"+userBean.getEmail(),userBean.getEmail());
            template.opsForValue().set("lockedDown:"+userBean.getEmail(),userBean.getEmail(),1800,TimeUnit.SECONDS);
        }
        map.put("success",true);
        map.put("errorNumber",number+1);
        return map;
    }


}
